package abel.wlmr.model.unit;

import java.util.ArrayList;
import java.util.List;

import abel.wlmr.model.WlmrBasePart;
import abel.wlmr.model.WlmrBaseUnit;
import abel.wlmr.model.WlmrDefaultUnit;
import abel.wlmr.model.WlmrException;
import abel.wlmr.model.type.DataIdBody;
import io.netty.buffer.ByteBuf;

public class AfnFirstPart extends WlmrBasePart {

    public static final short F_METER_STATUS = 402;
    public static final short F_VELOCITY = 403;
    public static final short F_VOLUME = 404;

    public AfnFirstPart() {
        super();
    }

    public AfnFirstPart(short guessedLen) {
        super(guessedLen);
    }

    @Override
    public int parseBuf(ByteBuf buf) throws WlmrException {
        id = new DataIdBody();
        id.parseBuf(buf);

        short len = (short) (getGuessedLength() - 4);

        for (short da : id.getDa().addrList()) {
            // same DA.
            List<WlmrBaseUnit> units = new ArrayList<>();
            unitList.add(units);

            // DT list for one DA.
            for (short dt : id.getDt().typeList()) {

                DataIdBody outerId = new DataIdBody();
                outerId.getDa().setAddrList(da);
                outerId.getDt().setTypeList(dt);

                switch (dt) {
                case F_METER_STATUS:

                {
                    AfnFirstF402Unit unit = new AfnFirstF402Unit(len);
                    unit.setOuterId(outerId);

                    int plen = unit.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0C-F402.");
                    }

                    len -= plen;
                    units.add(unit);

                    break;
                }
                case F_VELOCITY:

                {
                    AfnFirstF403Unit unit = new AfnFirstF403Unit(len);
                    unit.setOuterId(outerId);

                    int plen = unit.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0C-F403.");
                    }

                    len -= plen;
                    units.add(unit);

                    break;
                }
                case F_VOLUME:

                {
                    AfnFirstF404Unit unit = new AfnFirstF404Unit(len);
                    unit.setOuterId(outerId);

                    int plen = unit.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0C-F404.");
                    }

                    len -= plen;
                    units.add(unit);

                    break;
                }

                default:

                {
                    WlmrBaseUnit unit = new WlmrDefaultUnit(len);
                    unit.setOuterId(outerId);

                    len -= unit.parseBuf(buf);
                    units.add(unit);

                    break;
                }
                }
            }
        }

        return getGuessedLength() - len;
    }
}
